// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

// web 代码编辑器
import ace from 'ace-builds'

// 引入拼音库
import pinyin from 'js-pinyin'

// 时间操作库
import moment from 'moment'

// layer 弹窗 https://www.npmjs.com/package/vue-layer
import layer from 'vue-layer'

import { get } from './core/request/get'
import { post } from './core/request/post'
import { load } from './core/request/load'
import { jpost } from './core/request/jpost'
import { upload } from './core/request/upload'
import store from './store'

// 公共css
import './assets/global.css'
import './assets/backstyle.css'

// 引入百度 ueditor
import '../static/ueditor/ueditor.config.js'
import '../static/ueditor/ueditor.all.js'
import '../static/ueditor/lang/zh-cn/zh-cn.js'
import '../static/ueditor/ueditor.parse.min.js'

// 引入vue-lazyload
import VueLazyload from 'vue-lazyload'
import ImagesEmpty from '@/assets/images/image-empty.png'
import ImagesLoading from '@/assets/images/images-loading.gif'

// 引入vxe-table
import XEUtils from 'xe-utils'
import {VXETable, Header, Column, Table} from 'vxe-table'
// import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'
import zhCN from 'vxe-table/lib/locale/lang/zh-CN'

// 完整引入
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './assets/element-variables.scss'
Vue.use(ElementUI)

// 引入vxe-table
// Vue.use(VXETable)
// 按需加载的方式默认是不带国际化的，自定义国际化需要自行解析占位符 '{0}'，例如：
VXETable.setup({
  i18n: (key, args) => XEUtils.toFormatString(XEUtils.get(zhCN, key), args)
})
Vue.use(Header).use(Column).use(Table)

// 引入代码编辑器
Vue.use(ace)

// 使用vue-lazyload
Vue.use(VueLazyload, {
  preLoad: 1.3,
  error: ImagesEmpty,
  loading: ImagesLoading,
  attempt: 1
})

Vue.prototype.$pinyin = pinyin

// 定义layer全局变量
Vue.prototype.$layer = layer(Vue)
//  layer 全局参数重置
Vue.prototype.$layer = layer(Vue, {
  msgtime: 3 // 目前只有一项，即msg方法的默认消失时间，单位：秒
})

Vue.config.productionTip = false

Vue.prototype.$get = get
Vue.prototype.$post = post
Vue.prototype.$load = load
Vue.prototype.$jpost = jpost
Vue.prototype.$upload = upload
Vue.prototype.$moment = moment

// 定义时间格式化全局过滤器，filter两个参数 第一个是函数名  第二个是时间格式化处理的函数
Vue.filter('dateformat', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  if (dataStr) {
    return moment(dataStr).format(pattern)
  }
  return ''
})

// 禁止右键
Vue.directive('preventright', {
  bind: function (el, binding, vnode) {
    el.oncontextmenu = function () { return false }
  }
})

// 时间选择器 - 输入格式转换
Vue.directive('datetimeFormat', {
  inserted: function (el, binding, vnode) {
    const {value: _obj} = binding
    const {context: _this, data} = vnode
    const { expression: key } = data.model
    const evil = function (fn) {
      let Fn = Function
      return new Fn('return ' + fn)()
    }
    const modelValue = function (value, len) {
      value = value.replace(/[^0-9]/g, '')
      if (value.length > 5 && value.length < 9) {
        value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3')
        let dateN = new Date()
        let h = dateN.getHours()
        h = h < 10 ? ('0' + h) : h
        let minute = dateN.getMinutes()
        minute = minute < 10 ? ('0' + minute) : minute
        let second = dateN.getSeconds()
        second = second < 10 ? ('0' + second) : second
        value = value + ' ' + h + ':' + minute + ':' + second
      } else if (value.length > 9 && value.length < 13) {
        value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3 $4:$5:00')
      } else if (value.length > 12) {
        value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3 $4:$5:$6')
      } else {
        return false
      }
      let dateN
      if (!value || !value.constructor === String) {
        dateN = new Date()
      } else {
        dateN = evil('new Date(' + value.replace(/\d+(?=-[^-]+$)/, function (a) { return parseInt(a, 10) - 1 }).match(/\d+/g) + ')')
      }
      let y = dateN.getFullYear()
      let m = dateN.getMonth() + 1
      m = m < 10 ? ('0' + m) : m
      let d = dateN.getDate()
      d = d < 10 ? ('0' + d) : d
      let h = dateN.getHours()
      h = h < 10 ? ('0' + h) : h
      let minute = dateN.getMinutes()
      minute = minute < 10 ? ('0' + minute) : minute
      let second = dateN.getSeconds()
      second = second < 10 ? ('0' + second) : second
      value = y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second

      const time = moment(value, moment.ISO_8601)
      let keys = key.split('.')
      // 自定义赋值
      if (_obj) {
        const { keys: keyList } = _obj
        keys = keyList
      }

      if (keys && keys.length >= 2) {
        const [key1, key2, key3, key4] = keys

        if (key4) {
          _this[key1][key2][key3][key4] = time
        } else if (key3) {
          _this[key1][key2][key3] = time
        } else {
          _this[key1][key2] = time
        }
      } else {
        _this[key] = time
      }
    }
    if (_this && _this._isVue) {
      el.onchange = function () {
        let value = el.children[0].value
        modelValue(value, 1)
      }
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>',
  router,
  store
})
